<?php

/**
 * This is the model class for table "{{transactiondetails}}".
 *
 * The followings are the available columns in table '{{transactiondetails}}':
 * @property integer $idtransactiondetails
 * @property integer $idtransactionmaster
 * @property string $idacledger
 * @property string $transactionamount
 * @property string $tdpercentage
 * @property string $transactioncrdr
 *
 * The followings are the available model relations:
 * @property Acledger $idacledger0
 * @property Transactionmaster $idtransactionmaster0
 */
class Transactiondetails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Transactiondetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{transactiondetails}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idtransactionmaster, idacledger, transactionamount, transactioncrdr', 'required'),
			array('idtransactionmaster', 'numerical', 'integerOnly'=>true),
			array('idacledger, transactionamount, tdpercentage', 'length', 'max'=>10),
			array('transactioncrdr', 'length', 'max'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idtransactiondetails, idtransactionmaster, idacledger, transactionamount, tdpercentage, transactioncrdr', 'safe', 'on'=>'search'),
                        array('acledgername', 'autocompleteValidator', 'model'=>'Acledger'),
                        array('transactionmastervoucherdate', 'autocompleteValidator', 'model'=>'Transactionmaster'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!$params['model']::model()->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['acledgername'])){
                $match = addcslashes($_REQUEST['acledgername'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'acledgername = :acledgername',
                    'params'    => array(':acledgername' => "$match")
                ) );            
                $model=Acledger::model()->find($criteria);
                $this->idacledger = $model->idacledger;
            }
                        if(isset($_REQUEST['transactionmastervoucherdate'])){
                $match = addcslashes($_REQUEST['transactionmastervoucherdate'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'transactionmastervoucherdate = :transactionmastervoucherdate',
                    'params'    => array(':transactionmastervoucherdate' => "$match")
                ) );            
                $model=Transactionmaster::model()->find($criteria);
                $this->idtransactionmaster = $model->idtransactionmaster;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idacledger0' => array(self::BELONGS_TO, 'Acledger', 'idacledger'),
			'idtransactionmaster0' => array(self::BELONGS_TO, 'Transactionmaster', 'idtransactionmaster'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idtransactiondetails' => Yii::t('application', 'Idtransactiondetails'),
			'idtransactionmaster' => Yii::t('application', 'Idtransactionmaster'),
			'idacledger' => Yii::t('application', 'Idacledger'),
			'transactionamount' => Yii::t('application', 'Transactionamount'),
			'tdpercentage' => Yii::t('application', 'Tdpercentage'),
			'transactioncrdr' => Yii::t('application', 'Transactioncrdr'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idtransactiondetails',$this->idtransactiondetails);
		$criteria->compare('idtransactionmaster',$this->idtransactionmaster);
		$criteria->compare('idacledger',$this->idacledger,true);
		$criteria->compare('transactionamount',$this->transactionamount,true);
		$criteria->compare('tdpercentage',$this->tdpercentage,true);
		$criteria->compare('transactioncrdr',$this->transactioncrdr,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}